package com.dhcc.DRGsHosp.application.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.inject.Named;

import org.dayatang.domain.EntityRepository;
import org.dayatang.domain.InstanceFactory;
import org.dayatang.querychannel.JdbcTemplateObj;
import org.dayatang.querychannel.QueryChannelService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import com.dhcc.DRGsHosp.application.QuotaManageDataApplication;
@Named
@Transactional
public class QuotaManageDataApplicationImpl implements QuotaManageDataApplication {
	   @Autowired  
	   private JdbcTemplateObj jdbcTemplateObj;
	   
		private EntityRepository repository;

		private EntityRepository getRepository() {
			if (repository == null) {
				repository = InstanceFactory.getInstance(
						EntityRepository.class, "repository");
			}
			return repository;
		}
		private QueryChannelService queryChannel;
		
	    private QueryChannelService getQueryChannelService(){
	       if(queryChannel==null){
	          queryChannel = InstanceFactory.getInstance(QueryChannelService.class,"queryChannel");
	       }
	     return queryChannel;
	    }

	@Override
	public Map<String, Object> createDepDrgQuota() {
		Map<String, Object> map=new HashMap<>();
		List<Map<String, Object>> list1= jdbcTemplateObj.queryForList("select t.pardep_code depcode,t.parquota_code quotacode,t.manage_benchmark avalue from drgs.T_QUOTAMANAGE t where t.par_ms_code='manager_inner' and t.manage_year='2018' and t.pardep_code is not null and t.pardrg_code is null");	
		List<Map<String, Object>> list2= jdbcTemplateObj.queryForList("SELECT as_drg_code DRGCODE FROM DRGS.t_Annualstd std where as_year='2014' ");
		map.put("deplist", list1);
		map.put("drglist", list2);
		return map;
		
		
	}

	@Override
	public void insertData(String depcode, String drgCode, String quotaCode, Double qvalue) {
		String  sql="insert into drgs.t_quotamanage  (pardep_code,pardrg_code,parquota_code,manage_benchmark,manage_year,par_ms_code,qm_id) "
				+ "values('"+depcode+"','"+drgCode+"','"+quotaCode+"','"+qvalue+"','2018','manager_inner',sys_guid())";
		getRepository().createSqlQuery(sql.toString()).executeUpdate();
		
	}

	@Override
	public List<Map<String, Object>> getDrgByDepcode(String depcode) {
		
		return jdbcTemplateObj.queryForList("SELECT distinct p.pardrg_code DRGCODE  FROM DRGS.Mr_Ipinfo ip left join drgs.t_patientdrg p on ip.pmainindex=p.parid\r\n" + 
				" where p.pardrg_code is not null and ip.P00300001='"+depcode+"'");
	} 
}
